package ar.com.larreta.reportes;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;

import org.apache.log4j.Logger;

public class XLS extends TipoReporte {
	
	/** The Constant log. */
	private static final Logger log = Logger.getLogger(XLS.class);
	
	public static final String XLS_CONTENT_TYPE = "application/vnd.ms-excel";

	public XLS(UtilidadesReportes reportsUtils, String path) {
		super(reportsUtils, path);
	}
	
	@Override
	public ByteArrayOutputStream getOutputStream(JRDataSource dataSource) throws IOException {
		JasperPrint print = getPrint(dataSource);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			//	Seteo los parametros al exportador de XLS
			JRXlsExporter exporter = new JRXlsExporter ();
			log.debug("JRXLsExporter has been generated.");
			exporter.setParameter (JRExporterParameter.JASPER_PRINT, print);
			exporter.setParameter (JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
			exporter.setParameter (JRExporterParameter.OUTPUT_STREAM, baos);
			log.debug("Before export report to xls.");
			exporter.exportReport();
			log.debug("After export report to xls.");
		} catch (JRException e) {
			log.error(e);
		}
		return baos;
	}

	@Override
	public String getContentType() {
		return XLS_CONTENT_TYPE;
	}

}
